package org.finesys.common.redis.config;

import org.finesys.common.redis.core.cache.properties.CacheRedisCaffeineProperties;
import org.finesys.common.redis.core.cache.support.CacheMessageConsumer;
import org.finesys.common.redis.core.cache.support.RedisCaffeineCacheManager;
import org.finesys.common.redis.core.mq.client.RedisMQTemplate;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;

import lombok.RequiredArgsConstructor;

/**
 * 多级缓存
 */

@ConditionalOnProperty(prefix = "redis.cache.multi", name = "enabled", havingValue = "true", matchIfMissing = true)
@RequiredArgsConstructor
@AutoConfiguration
@AutoConfigureAfter(RedisAutoConfiguration.class)
@EnableConfigurationProperties(CacheRedisCaffeineProperties.class)
public class CacheRedisCaffeineAutoConfiguration {

	private final CacheRedisCaffeineProperties cacheRedisCaffeineProperties;

	@Bean
	public CacheManager redisCaffeineCacheManager(RedisTemplate<String,Object> redisTemplate, RedisMQTemplate redisMQTemplate) {
		return new RedisCaffeineCacheManager(cacheRedisCaffeineProperties, redisTemplate, redisMQTemplate);
	}

	@Bean
	public CacheMessageConsumer cacheMessageConsumer(CacheManager cacheManager) {
		return new CacheMessageConsumer(cacheManager);
	}

}
